import React, { Component } from 'react';
import { CasContext, checkCookieTicket, checkUrlTicket } from './cas.js';
class CasWrapper extends Component {
  constructor(props) {
    super(props);
    this.state = {
      ticket: '',
      user: {},
      loading: true,
    };
  }

  async componentDidMount() {
    let re = await checkCookieTicket();
    if (re) {
      this.setState({
        ...re,
        loading: false,
      });
    } else {
      re = await checkUrlTicket();
      if (re) {
        this.setState({
          ...re,
          loading: false,
        });
      } else {
        this.setState({
          loading: false,
        });
      }
    }
  }
  render() {
    const { children } = this.props;
    const { loading, ...value } = this.state;
    return <CasContext.Provider value={value}>{!loading && children}</CasContext.Provider>;
  }
}
export default CasWrapper;
